import binascii
import struct
from pathlib import Path
from typing import *


class _DataSaver:
    """The global data saver, singleton """

    def __init__(self) -> None:
        self.__desc: list[str] = []
        self.__format: list[str] = []
        self.__value: list[Any] = []
        
    def add(self, desc: str, format: str, value: Any) -> None:
        self.__desc.append(desc)
        self.__format.append(format)
        self.__value.append(value)
        
    def clear(self) -> None:
        self.__desc.clear()
        self.__format.clear()
        self.__value.clear()
    
    def pack(self) -> bytes:
        return binascii.hexlify(
            struct.pack(" ".join(self.__format), *self.__value)
        )
    
    def tofile(self, filepath: Path) -> None:
        fhand = open(filepath, "w", encoding="utf8")      
        
        desc = ",".join(self.__desc)
        fhand.write(desc + "\n")
        
        format = " ".join(self.__format)
        fhand.write(format + "\n")

        fhand.write(self.pack().decode() + "\n")
        
        fhand.close()

DataSaver = _DataSaver()

class _DataLoader:
    """The global data loader, singleton """
    
    
    
DataLoader = _DataLoader()
